package top.mcwebsite.playandroid.common.network

import top.mcwebsite.playandroid.common.network.model.RespModel

open class ApiException(
    val errorCode: Int,
    val errorMsg: String
): RuntimeException() {


}

class BizApiException(
    errorCode: Int,
    errorMsg: String,
    val data: RespModel<*>? = null,
): ApiException(errorCode, errorMsg) {
    companion object {
        const val UnLogin = -1001
        const val UNKNOWN = Int.MIN_VALUE
    }
}

fun BizApiException.Companion.unknown(errorMsg: String? = null) =
    BizApiException(UNKNOWN, errorMsg ?: "", null)

class NetworkApiException(
    errorCode: Int,
    errorMsg: String,
    cause: Throwable? = null
) : ApiException(errorCode, errorMsg) {
    companion object {
        const val Timout = 1000
        const val NoInternet = 2000
        const val ClientError = 400
        const val ServerError = 500
        const val Unknown = Int.MIN_VALUE
    }
}

fun NetworkApiException.Companion.timeout(cause: Throwable?) =
    NetworkApiException(errorCode = Timout, errorMsg = "请求超时", cause = cause)

fun NetworkApiException.Companion.unknown(cause: Throwable? = null) =
    NetworkApiException(errorCode = Unknown, errorMsg = "未知错误", cause = cause)

fun NetworkApiException.Companion.noInternet() =
    NetworkApiException(errorCode = NoInternet, errorMsg = "没有网络连接")

// TODO
fun NetworkApiException.Companion.clientError(httpStatusCode: Int) =
    NetworkApiException(errorCode = ClientError, errorMsg = "客户端错误")

fun NetworkApiException.Companion.serverError(httpStatusCode: Int) =
    NetworkApiException(errorCode = ServerError, errorMsg = "服务器出错啦")